#/usr/bin/env python
from PIL import Image
import os
import time
import sys

if len(sys.argv) < 2:
	print("No files provided", file=sys.stderr)
	sys.exit(2)

quality = input("Quality (0-100 or 'maximum', default 95)> ")
if quality == "":
	quality = 95
elif quality != "maximum":
	quality = int(quality)

for filename in sys.argv[1:]:
	assert os.path.exists(filename)
	new_name = os.path.splitext(filename)[0] + ".jpg"
	print("%r --> %r" % (filename, new_name))
	if os.path.exists(new_name):
		r = ""
		while r != "y" and r != "n":
			r = input("File exists, overwrite? [y/n]> ")
		if r == "n": continue
	img = Image.open(filename)
	img.convert("RGB").save(new_name, quality=quality)
	try:
		os.utime(new_name, (time.time(), os.stat(filename).st_mtime))
	except Exception as e:
		print(e)
